﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using ZL.Push;
using ZL.Push.Web;


namespace ZL.Push
{
    public partial class FriendWindow : ChildWindow
    {
        public FriendWindow()
        {
            InitializeComponent();
        }

        public FriendWindow(AddOrEditType type)
        {
            InitializeComponent();

            switch (type)
            {
                case AddOrEditType.eAdd:
                    this.Title = "添加好友";
                    break;
                case AddOrEditType.eEdit:
                    this.Title = "编辑好友";
                    break;
                default:
                    this.Title = "好友信息";
                    _btn_ok.IsEnabled = false;
                    break;
            }
        }

        public void init_category_combobox()
        {
            tbl_friend tbl = this.DataContext as tbl_friend;

            _combobox_category.Items.Add( new ComboBoxItem()
            {
                Content = new StackPanel()
                {
                    Orientation = System.Windows.Controls.Orientation.Horizontal,
                    Children = { 
                        new Image()
                        {
                            Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../Icon/tree_friend_ungroup_cg.png", UriKind.Relative)), 
                        },
                        new TextBlock()
                        {
                            Margin = new Thickness(2, 0, 0, 0),
                            Text = "--",
                            VerticalAlignment = System.Windows.VerticalAlignment.Center,
                        } }
                },
                Tag = 0,
                IsSelected = tbl.category_id == 0,
            });

            if (App.s_dict_friend_name_path != null)
            {
                foreach (KeyValuePair<int, string> pair in App.s_dict_friend_name_path)
                {
                    _combobox_category.Items.Add(new ComboBoxItem()
                    {
                        Content = new StackPanel()
                        {
                            Orientation = System.Windows.Controls.Orientation.Horizontal,
                            Children = { 
                            new Image()
                            {
                                Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("../Icon/tree_friend_cg.png", UriKind.Relative)), 
                            },
                            new TextBlock()
                            {
                                Margin = new Thickness(2, 0, 0, 0),
                                Text = pair.Value,
                                VerticalAlignment = System.Windows.VerticalAlignment.Center,
                            } }
                        },
                        Tag = pair.Key,
                        IsSelected = tbl.category_id == pair.Key,
                    });
                }
            }
        }

        private void _btn_ok_Click(object sender, RoutedEventArgs e)
        {
            tbl_friend tbl = this.DataContext as tbl_friend;
            if (string.IsNullOrEmpty(tbl.nick_name))
            {
                MessageBox.Show("好友昵称不能为空");
                return;
            }

            ComboBoxItem item = _combobox_category.SelectedItem as ComboBoxItem;
            if (item != null)
            {
                tbl.category_id = Convert.ToInt32(item.Tag);
            }
            else
            {
                tbl.category_id = 0;
            }

            this.DialogResult = true;
        }

        private void _btn_cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}